package com.zxx.order.ui.activity

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bigkoo.alertview.AlertView
import com.bigkoo.alertview.OnItemClickListener
import com.eightbitlab.rxbus.Bus
import com.kennyc.view.MultiStateView
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ext.startLoading
import com.zqq.baselibrary.ui.activity.BaseMvpActivity
import com.zqq.baselibrary.ui.adapter.BaseRecyclerViewAdapter
import com.zqq.provider.router.RouterPath
import com.zxx.order.R
import com.zxx.order.common.OrderConstant
import com.zxx.order.event.SelectAddressEvent
import com.zxx.order.injection.component.DaggerShipAddressComponent
import com.zxx.order.injection.module.ShipAddressModule
import com.zxx.order.presenter.ShipAddressPresenter
import com.zxx.order.presenter.iview.ShipAddressView
import com.zxx.order.service.bean.ShipAddress
import com.zxx.order.ui.adapter.ShipAddressAdapter
import kotlinx.android.synthetic.main.activity_ship_address.*
import org.jetbrains.anko.toast

/**
 * 收货人信息列表页
 * wc
 */
@Route(path = RouterPath.OrderCenter.PATH_SHIP_ADDRESS)
class ShipAddressActivity : BaseMvpActivity<ShipAddressPresenter>(), ShipAddressView {

    private lateinit var mAdapter: ShipAddressAdapter

    override fun injectComponent() {
        DaggerShipAddressComponent.builder()
                .activityComponent(mActivityComponent)
                .shipAddressModule(ShipAddressModule())
                .build()
                .inject(this)
        mPresenter.mView = this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ship_address)

        initView()
    }

    override fun onStart() {
        super.onStart()
        loadData()
    }

    //初始化视图
    private fun initView() {
        mAddressRv.layoutManager = LinearLayoutManager(this)
        mAdapter = ShipAddressAdapter(this)
        mAddressRv.adapter = mAdapter

        //设置操作事件(adapter接口回调)
        mAdapter.mOptClickListener = object : ShipAddressAdapter.OnOptClickListener {
            override fun onSetDefault(address: ShipAddress) {
                mPresenter.setDefaultShipAddress(address)
            }

            override fun onEdit(address: ShipAddress) {
                ARouter.getInstance()
                        .build(RouterPath.OrderCenter.PATH_SHIPEDIT_ADDRESS)
                        //需要序列化
                        .withParcelable(OrderConstant.KEY_SHIP_ADDRESS, address)
                        .navigation()
            }

            override fun onDelete(address: ShipAddress) {
                AlertView("删除", "确定删除该地址？", "取消",
                        null, arrayOf("确定"), this@ShipAddressActivity,
                        AlertView.Style.Alert, OnItemClickListener { o, position ->
                    if (position == 0) {
                        mPresenter.deleteShipAddress(address.id)
                    }
                }).show()
            }
        }

        //单项点击事件--代表选中了当前地地址
        mAdapter.setOnItemClickListener(object : BaseRecyclerViewAdapter.OnItemClickListener<ShipAddress> {
            override fun onItemClick(item: ShipAddress, position: Int) {
                Bus.send(SelectAddressEvent(item))
//                EventBus.getDefault().post(SelectAddressEvent(item))
                finish()
            }
        })

        //新建地址
        mAddAddressBtn.onClick {
            ARouter.getInstance().build(RouterPath.OrderCenter.PATH_SHIPEDIT_ADDRESS)
                    .navigation()
        }

    }


    //加载数据
    private fun loadData() {
        mMultiStateView.startLoading()
        mPresenter.getShipAddressList()
    }


    //-----------接口返回---------------
    //获取收货人信息回调
    override fun onGetShipAddressResult(result: MutableList<ShipAddress>?) {
        if (result != null && result.size > 0) {
            mAdapter.setData(result)
            mMultiStateView.viewState = MultiStateView.VIEW_STATE_CONTENT
        } else {
            mMultiStateView.viewState = MultiStateView.VIEW_STATE_EMPTY
        }
    }

    //设置默认收货人回调
    override fun onSetDefaultResult(result: Boolean) {
        toast("设置默认成功")
        loadData()
    }

    //删除收货人回调
    override fun onDeleteResult(result: Boolean) {
        toast("删除成功")
        loadData()
    }

}
